package com.hotel.project.demo.service.impl;

import com.hotel.project.demo.config.RedisConfig;
import com.hotel.project.demo.dao.hotel.HotelDao;
import com.hotel.project.demo.domain.HotelEntity;
import com.hotel.project.demo.domain.HotelPictureEntity;
import com.hotel.project.demo.domain.common.HotelEvaluate;
import com.hotel.project.demo.service.HotelService;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HotelServiceImpl extends CurdServiceImpl<HotelEntity> implements HotelService {
    private JedisPool jedisPool = (new RedisConfig()).getJedisPool();
    private Jedis jedis = null;

    /**
     * 新增酒店
     * @param entity
     * @return
     */
    @Override
    public boolean save(HotelEntity entity) {
        Map<String,String> map = new HashMap<>();
        Boolean b = false;
        try {
            if (((HotelDao)getBaseMapper()).insert(entity) == 0){
                return false;
            }
            jedis = jedisPool.getResource();
            map.put("evaluateScore","0");
            map.put("evaluateCount","0");
            map.put("saleCount","0");
            map.put("collectCount","0");
            map.put("deleted","1");
            jedis.hmset(entity.getName(),map);
            b = true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            jedis.close();
        }
        return b;
    }

    /**
     * 更新酒店缓存
     * @param id
     * @param hotelName
     * @return
     */
    @Override
    public boolean updateHotelCache(Long id,String hotelName) {
        Boolean b = false;
        jedis = jedisPool.getResource();
        Map<String,String> map = new HashMap<>();
        try {
            HotelEvaluate hotelEva = ((HotelDao)getBaseMapper()).getHotelEva(id);
            Float score = hotelEva.getScore();
            Integer counts = hotelEva.getCounts();
            List<String> CacheStr = jedis.hmget(hotelName,"evaluateScore","evaluateCount","saleCount","collectCount");
            counts += Integer.parseInt(CacheStr.get(1));
            score =Float.parseFloat (new DecimalFormat("#.0").format((score + Float.parseFloat(CacheStr.get(0))/counts)));
            Integer saleCount = Integer.parseInt(CacheStr.get(2));
            Integer collectCount = Integer.parseInt(CacheStr.get(3));
            int temp = ((HotelDao)getBaseMapper()).updateHotelCache(id,score,counts,saleCount,collectCount);
            if (temp > 1){
                map.put("evaluateScore","0");
                map.put("evaluateCount","0");
                map.put("saleCount","0");
                map.put("collectCount","0");
                jedis.hmset(hotelName,map);
                b = true;
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            jedis.close();
        }
        return b;
    }

    /**
     * 将用户评分存入缓存
     * @param hotelName
     * @param evaluateScore
     * @return
     */
    @Override
    public boolean updateHotelEvaluateCache(String hotelName,Integer evaluateScore) {
        Boolean b = false;
        Map<String,String> map = new HashMap<>();
        try{
            jedis = jedisPool.getResource();
            List<String> list = jedis.hmget(hotelName,"evaluateScore","evaluateCount");
            String score = Integer.parseInt(list.get(0)) + evaluateScore + "";
            String count = Integer.parseInt(list.get(1)) + 1 + "";
            map.put("evaluateScore",score);
            map.put("evaluateCount",count);
            jedis.hmset(hotelName,map);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            jedis.close();
        }
        return b;
    }

    /**
     * 酒店销量缓存更新
     * @param hotelName
     * @param saleCount
     * @return
     */
    @Override
    public boolean updateHotelSaleCache(String hotelName, Integer saleCount) {
        Boolean b = false;
        Map<String,String> map = new HashMap<>();
        try{
            jedis = jedisPool.getResource();
            List<String> list = jedis.hmget(hotelName,"saleCount");
            String count = Integer.parseInt(list.get(0)) + 1 + "";
            map.put("saleCount",count);
            jedis.hmset(hotelName,map);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            jedis.close();
        }
        return b;
    }

    /**
     * 更新酒店收藏缓存
     * @param hotelName
     * @return
     */
    @Override
    public boolean updateHotelCollectCache(String hotelName,Integer collectCount) {
        Boolean b = false;
        Map<String,String> map = new HashMap<>();
        try{
            jedis = jedisPool.getResource();
            List<String> list = jedis.hmget(hotelName,"collectCount");
            String count = Integer.parseInt(list.get(0)) + collectCount + "";
            map.put("collectCount",count);
            jedis.hmset(hotelName,map);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            jedis.close();
        }
        return b;
    }

    /**
     * 修改酒店营业状态
     * @param id
     * @param deleted
     * @return
     */
    @Override
    public int updateHotelStatus(Long id,String hotelName,Integer deleted) {
        try {
            jedis = jedisPool.getResource();
            Map<String,String> map = new HashMap<>();
            int temp;
            if (deleted == 0){
                jedis.hdel(hotelName);
               temp = ((HotelDao)getBaseMapper()).updateHotelStatus(id,deleted);
            }else{
                List<String> list = jedis.hmget(hotelName);
                if (list.size() == 0){
                    map.put("evaluateScore","0");
                    map.put("evaluateCount","0");
                    map.put("saleCount","0");
                    map.put("collectCount","0");
                    jedis.hmset(hotelName,map);
                }
                temp = ((HotelDao)getBaseMapper()).updateHotelStatus(id,deleted);
            }
            if (temp > 0){
                return 1;
            }
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }finally {
            jedis.close();
        }
        return 0;
    }
}
